﻿using SiyinPractice.Application.Core;
using SiyinPractice.Domain.Core;
using SiyinPractice.Domain.Maintenance;
using SiyinPractice.Framework.Mapper;
using SiyinPractice.Interface.Maintenance;
using SiyinPractice.Shared.Core.Dto;
using SiyinPractice.Shared.Core.Exceptions;
using SiyinPractice.Shared.Core.Utility;
using SiyinPractice.Shared.Maintenance.Dto.AccountPassword;
using System.Linq.Expressions;

namespace SiyinPractice.Application.Maintenance
{
    public class AccountPasswordService : NamedEntityService<AccountPassword, AccountPasswordDto, AccountPasswordSearchPagedDto, CreateAccountPasswordDto>, IAccountPasswordService
    {
        public AccountPasswordService(IEfRepository<AccountPassword> repository, IObjectMapper objectMapper)
                  : base(repository, objectMapper)
        {
        }

        protected override async Task<AccountPasswordDto> MapToEntityDto(AccountPassword entity)
        {
            var dto = await base.MapToEntityDto(entity);
            if (dto != null)
            {
            }

            return dto;
        }

        protected override async Task<List<AccountPasswordDto>> MapToEntityDto(List<AccountPassword> entities)
        {
            var dtos = await base.MapToEntityDto(entities);
            if (dtos?.Any() == true)
            {
                foreach (var dto in dtos)
                {
                    var entity = entities.FirstOrDefault(x => x.Id == dto.Id);
                }
            }
            return dtos;
        }

        public override async Task<AccountPasswordDto> AddAsync(CreateAccountPasswordDto createInput)
        {
            Validate.Assert(createInput == null, SiyinPracticeMessage.DTO_IS_NULL);
            //var exits = await Repository.AnyAsync(x => x.Name == createInput.Name);
            //Validate.Assert(exits, SiyinPracticeMessage.ENTITY_EXIST, createInput.AccountName);
            var entity = await MapToEntity(createInput);
            if (entity is AuditEntity auditEntity)
            {
                auditEntity.Creator = Framework.Security.UserTokenService.GetUserToken().UserName;
                auditEntity.CreateTime = DateTime.Now;
            }
            await Repository.InsertAsync(entity);

            return await MapToEntityDto(entity);
        }

        public async Task<List<AccountPasswordDto>> GetAccountPasswordDto(string MsgReminderType)
        {
            //今年一月一日
            List<AccountPasswordDto> list = new List<AccountPasswordDto>();
            var repository = string.IsNullOrEmpty(MsgReminderType) ? Repository.Where(x => x.IsReminder != true).ToList() : Repository.Where(x => x.IsReminder != true && x.MsgReminderType == MsgReminderType).ToList();
            list = repository == null ? list : await MapToEntityDto(repository);
            return list;
        }

        public async Task<int> UpdateAccountPassword(Guid id)
        {
            //今年一月一日
            var entity = await Repository.FindAsync(id);
            Validate.Assert(entity == null, "数据不存在");
            entity.IsReminder = true;
            //entity.Editor = Framework.Security.UserTokenService.GetUserToken().UserName;
            //entity.EditTime = DateTime.Now;
            return await Repository.UpdateAsync(entity);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public virtual async Task<PageModelDto<AccountPasswordDto>> GetPagedAsync(AccountPasswordSearchPagedDto search)
        {
            var whereExpression = BuildWhereExpression(ExpressionCreator.New<AccountPassword>(), search);

            var total = await Repository.CountAsync(whereExpression);
            if (total == 0) return new PageModelDto<AccountPasswordDto>(search);

            var includeProperties = BuildIncludeProperties();

            var entities = Repository.Where(whereExpression, includeProperties)
                                     .OrderBy(x => x.Id)
                                     .Skip(search.SkipRows())
                                     .Take(search.PageSize).OrderBy(x => x.Name)
                                     .ToList();

            var entityDtos = await MapToEntityDto(entities);
            return new PageModelDto<AccountPasswordDto>(search, entityDtos, total);
        }

        //public async Task<List<AccountPasswordDto>> GetByInvoiceHistoryId(Guid invoiceHistoryId)
        //{
        //    var entity = Repository.Where(x => x.InvoiceHistoryId == invoiceHistoryId).ToList();
        //    Validate.Assert(entity == null, "数据不存在");
        //    return await MapToEntityDto(entity);
        //}
    }
}